package com.gshocklab.bulkposter.view.adapter;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gshocklab.bulkposter.AppMainActivity;
import com.gshocklab.bulkposter.R;
import com.gshocklab.bulkposter.data.dto.SocialAccount;

/**
 * Adapter for display already registered social netrowrk's accounts
 * @author vitaly gashock
 */

public final class AccountsListAdapter extends ArrayAdapter<SocialAccount> {
	private Activity context;
	private List<SocialAccount> accountsList;
	private LayoutInflater layoutInflater;
	
	public AccountsListAdapter(Activity context, List<SocialAccount> accountsList) {
		super(context, R.layout.account_item, accountsList);
		
		this.accountsList = accountsList;
		this.context = context;
		this.layoutInflater = context.getLayoutInflater();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemViewHolder itemViewHolder = null;
		final SocialAccount socialAccoint = accountsList.get(position);
		
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.account_item, parent, false);
			itemViewHolder = new ItemViewHolder();
			
			itemViewHolder.imIcon = (ImageView) convertView.findViewById(R.id.icon_item);
			itemViewHolder.tvUsername = (TextView) convertView.findViewById(R.id.text_username_item);
			itemViewHolder.tvAccountType = (TextView) convertView.findViewById(R.id.text_account_type_item);
			
			convertView.setTag(itemViewHolder);			
		} else {
			itemViewHolder = (ItemViewHolder) convertView.getTag();
		}
		
		itemViewHolder.imIcon.setImageResource(socialAccoint.type.bigIconId);
		itemViewHolder.tvUsername.setText(socialAccoint.username);
		itemViewHolder.tvAccountType.setText(socialAccoint.type.title);
		
		if (context instanceof AppMainActivity) {
			itemViewHolder.tvUsername.setTextColor(Color.BLACK);
			itemViewHolder.tvAccountType.setTextColor(Color.BLACK);
		}
		
		return convertView;
	}
	
	private class ItemViewHolder {
		public ImageView imIcon;
		public TextView tvUsername;
		public TextView tvAccountType;
	}
}
